package cj.web.portal.controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.kly.domain.Columns;
import cj.web.kly.domain.News;
import cj.web.kly.domain.SourceInfor;
import cj.web.kly.service.SourceInforService;
import cj.web.login.controllers.BaseController;
import cj.web.portal.service.ColumnsNewsService;

/**
 * 新闻动态、科技动态、党群之窗、通知公告
 * @author Administrator
 *
 */
@Path("columnsNews")
public class ColumnsNewsController extends BaseController {
	
	private static Logger logger = Logger.getLogger(ColumnsNewsController.class.getName());
	
	public static String SOURCE_CODE = "SOURCE_CODE";
	public static String NO_SHOW_TIME = "NO_SHOW_TIME";
	
	@Autowired
	private ColumnsNewsService columnsNewsService;
	@Autowired
	private SourceInforService sourceInforService;
	
	/**
	 * 栏目页
	 * @return
	 * @throws Exception 
	 */
	@Get("newsListPage")
	public String newsListPage(Model model,@Param("columnsId") int columnsId
			,HttpServletRequest request,Invocation inv) throws Exception {
		ServletContext context = inv.getServletContext();  
        HashMap dictionaryInfoMap = (HashMap)context.getAttribute("dictionaryInfoMap");
		Map rep = (Map) dictionaryInfoMap.get("configRep");
		String news_columnsid=rep.get(SOURCE_CODE).toString();//新闻动态 columnsId 3
		String no_show_time_columnsid=rep.get(NO_SHOW_TIME).toString();//不显示列表时间
		
		model.add("sourcelis","");
		for(int i=0;i<news_columnsid.split(",").length;i++){
			if(columnsId==Integer.parseInt(news_columnsid.split(",")[i])){
				List<SourceInfor> sourcelis = sourceInforService.queryAll();
				model.add("sourcelis",sourcelis);
			}
		}
		
		//查询栏目信息
		Columns columns = columnsNewsService.queryColumns(columnsId);
		model.add("columns",columns);
		model.add("columnsId",columnsId);
		
		List<Map> repHeadLinklis = returnHeadLink(columnsId);
		model.add("repHeadLinklis",repHeadLinklis);
		model.add("columnsMemuKey",repHeadLinklis.get(0).get("value"));
		model.add("columnsMemuCn",repHeadLinklis.get(0).get("name"));
		model.add("columnsMemuEn",repHeadLinklis.get(0).get("en"));
		
		boolean flag = false;
		
		for(int i=0;i<no_show_time_columnsid.split(",").length;i++){
			if(columnsId==Integer.parseInt(no_show_time_columnsid.split(",")[i])){
				flag=true;
				break;
			}
		}
		
		if(flag){
			return "/views/portal/columnsNews/msgListNoTime.vm";
		}else{
			return "/views/portal/columnsNews/msgList.vm";
		}
	}
	
	/**
	 * 请求方法
	 * @param columnsId
	 * @param start
	 * @param length
	 * @return
	 * @throws Exception 
	 */
	@Post("getNewsList")
	public String queryList(@Param("columnsId") int columnsId,@Param("source") String source,
			@Param("start") int start,@Param("length") int length) {
	
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		Map rep = columnsNewsService.query(columnsId,source, start*length, length);
		List<News> objs = (List<News>) rep.get("newslis");
		int size = (int) rep.get("size");
		jo.put("list", objs);
		jo.put("totalCount", size);
		
		return "@"+jo.toString();
	}
	
	/**
	 * 新闻详情
	 * @param model
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@Get("newsDetailPage")
	public String newsDetailPage(Model model,@Param("columnsId") int columnsId,@Param("id") int id) throws Exception {
		//查询栏目信息
		Columns columns = columnsNewsService.queryColumns(columnsId);
		
		News obj = columnsNewsService.queryById(id);
		
		List<News> newslis = new ArrayList<News>();
		newslis.add(obj);
		newslis.addAll(columnsNewsService.queryListById(columnsId,id, 0, 4));
		
		model.add("columns",columns);
		model.add("newslis",newslis);
		model.add("id",id);
		
		List<Map> repHeadLinklis = returnDetailLink(columnsId);
		model.add("repHeadLinklis",repHeadLinklis);
		model.add("columnsMemuKey",repHeadLinklis.get(0).get("value"));
		return "/views/portal/columnsNews/msgDetail.vm";
	}
	
	/**
	 * 新闻详情
	 * @param model
	 * @param id
	 * @return
	 * @throws ParseException
	 */
	@Post("newsDetail")
	public String newsDetail(@Param("id") int id) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		News obj = columnsNewsService.queryById(id);
		jo.put("obj", obj);
		return "@"+jo.toString();
	}
	
	/**
	 * 
	 * @param columnsId
	 * @return
	 * @throws Exception
	 */
	private List<Map> returnHeadLink(int columnsId) throws Exception{
		List<Map> replis = new ArrayList<Map>();
		String str = getParentName(columnsId);
		
		String[] heads = str.split("&");
		for(int i=1;i<heads.length-1;i++){
			Map rep = new HashMap();
			rep.put("name", heads[i].split("@")[0]);
			rep.put("value", heads[i].split("@")[1]);
			rep.put("en", heads[i].split("@").length>2?heads[i].split("@")[2]:"");
			replis.add(rep);
		}
		return replis;
	}
	
	/**
	 * 
	 * @param columnsId
	 * @return
	 * @throws Exception
	 */
	private List<Map> returnDetailLink(int columnsId) throws Exception{
		List<Map> replis = new ArrayList<Map>();
		String str = getParentName(columnsId);
		
		String[] heads = str.split("&");
		for(int i=1;i<heads.length;i++){
			Map rep = new HashMap();
			rep.put("name", heads[i].split("@")[0]);
			rep.put("value", heads[i].split("@")[1]);
			rep.put("en", heads[i].split("@").length>2?heads[i].split("@")[2]:"");
			replis.add(rep);
		}
		return replis;
	}
	
	/** 
     * 递归得到当前节点的所有父节点 
     * @param configId 当前节点 
     * @return 所有父节点 
     * @throws Exception 抛出的异常 
     */  
    private String getParentName(int columnsId)throws Exception{  
        //和数据库交互,得到当前节点记录  
    	Columns columnsEntity = columnsNewsService.queryColumns(columnsId);
        if(columnsEntity != null){  
            String configName = columnsEntity.getName()+"@"+columnsEntity.getValue()+"@"+columnsEntity.getEn()+"&";  
            //参数patrolConfigEntity.getConfigParentId()表示当前节点的父节点ID  
            String returnConfigName = getParentName(columnsEntity.getpId());  
            return returnConfigName+configName;  
        }else{  
            return "";  
        }  
    }
}